package com.example.sunmiv1.activity;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.sunmiv1.R;
import com.example.sunmiv1.adapter.DepositDetailAdapter;
import com.example.sunmiv1.entity.DepositDetailInfo;
import com.example.sunmiv1.utils.net.ACache;
import com.example.sunmiv1.utils.net.Common;
import com.example.sunmiv1.utils.net.GsonUtils;
import com.example.sunmiv1.utils.net.HttpUtils;
import com.example.sunmiv1.utils.net.LoadingDialog;
import com.example.sunmiv1.utils.net.TextCallBack;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class DepositDetailActivity extends Activity {


    @BindView(R.id.title_back)
    ImageView titleBack;
    @BindView(R.id.title_title)
    TextView titleTitle;
    @BindView(R.id.listView_depositDetail)
    ListView listViewDepositDetail;
    private ACache aCache;
    private int type;//1.DepositAdapter出庫詳情 2.DepositAdapter入庫詳情
    private Context context;
    private LoadingDialog loadingDialog;
    private DepositDetailInfo depositDetailInfo;
    private List<DepositDetailInfo.DataBean> list = new ArrayList<>();
    private DepositDetailAdapter depositDetailAdapter;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    for (DepositDetailInfo.DataBean dataBean : depositDetailInfo.getData()) {
                        list.add(dataBean);
                    }
                    setDepositDetailAdapter();
                    break;
                case 2:
                    setDepositDetailAdapter();
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_deposit_detail);
        ButterKnife.bind(this);
        titleTitle.setText(R.string.depositDetailTitle);
        context = this;
        aCache = ACache.get(context);
        loadingDialog = new LoadingDialog(context);
        type = getIntent().getIntExtra("type", 1);
        getDepositDetail();
    }


    @OnClick(R.id.title_back)
    public void onViewClicked() {
        finish();
    }

    private void setDepositDetailAdapter() {
        if (depositDetailAdapter == null) {
            depositDetailAdapter = new DepositDetailAdapter(context, list, type);
            listViewDepositDetail.setAdapter(depositDetailAdapter);
        } else {
            depositDetailAdapter.notifyDataSetChanged();
        }
    }

    private void getDepositDetail() {
        loadingDialog.show();
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("id", getIntent().getStringExtra("id"));
            jsonObject.put("storeid", aCache.getAsString("storeid"));
            jsonObject.put("merchantid", aCache.getAsString("merchantid"));
            if ("1".equals(aCache.getAsString("userType")) || "2".equals(aCache.getAsString("userType"))) {
                jsonObject.put("operator_id", aCache.getAsString("operator_id"));
            } else {
                jsonObject.put("operator_id", aCache.getAsString("uid"));
            }
            jsonObject.put("user_type", aCache.getAsString("userType"));
            HttpUtils.post(context, Common.ALLOCATE_INFO, jsonObject, new TextCallBack() {
                @Override
                protected void onSuccess(String text) {
                    loadingDialog.dismiss();
                    depositDetailInfo = GsonUtils.JsonToBean(text, DepositDetailInfo.class);
                    Message msg = new Message();
                    if (depositDetailInfo.getStatus() == 1) {
                        msg.what = 1;
                    } else {
                        msg.what = 2;
                    }
                    handler.sendMessage(msg);
                }

                @Override
                protected void onFailure(ResponseException e) {
                    loadingDialog.dismiss();
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}
